<?php
class Administrator_GameController extends Zend_Controller_Action
{
	public function init()
	{
		$member=new CMember();
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){	
			$this->_redirect(MY_URL.'/administrator-auth-login');
		}		
	}
	
	public function preDispatch()
	{
		$this->getHelper ('layout')->setLayout('framelayout');
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
	}
	
	public function indexAction()
	{
		$game = new CGame();
		$joinerList = $game->fetchAll();
		$this->view->joinerList = $joinerList;
		$this->view->joinerCount = count($joinerList);
		
		try{
			$config = new Zend_Config_Xml(MY_ROOT.'/html/upload/game/configs.xml');
			$config = $config->toArray();
		} catch (Exception $e) {
			die('error game configs');
		}
		$endTime = $config['EndingTime'];
		if (strtotime($endTime) > time()) {
			$this->view->isEnd = 0;
		} else {
			$this->view->isEnd = 1;
		}
	}
	
	public function codeAction()
	{
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		$expire = $this->getRequest()->getParam('expire', '');
		if (empty($expire)) {
			echo 'expire time';
			exit();
		}
		$game = new CGame();
		$joinerList = $game->fetchAll();
		foreach ($joinerList as $v) {
			$game->setHashCode($v, $expire);
		}
		$this->view->showMsg('生成成功','/administrator-game');
	}
	
	public function sendmailAction()
	{
		if (!$this->getRequest()->isPost()) {
			@$content = file_get_contents(MY_ROOT.'/html/upload/game/sendmail.txt');
			if (!empty($content)) {
				$arr = unserialize($content);
				$this->view->from = $arr['from'];
				$this->view->subject = $arr['subject'];
				$this->view->nickname = $arr['nickname'];				
			}
			$this->view->mailbody = isset($arr) ? str_replace('\\','',$arr['mailbody']) : '';
//			require_once (MY_ROOT."/fckeditor/fckeditor.php");
//			$oFCKeditor = new FCKeditor('mailbody') ;
//			$oFCKeditor->BasePath	= ROOT_PATH.'/fckeditor/' ;
//			$oFCKeditor->Value		= isset($arr) ? $arr['mailbody'] : '';
//			$oFCKeditor->Height=400;
//			$oFCKeditor->Width="98%";
//			$this->view->assign('oFCKeditor',$oFCKeditor);
//			echo str_replace('\\','',$arr['mailbody']);exit;
		} else {
			$from = $this->getRequest()->getParam('from', '');
			$subject = $this->getRequest()->getParam('subject', '');
			$nickname = $this->getRequest()->getParam('nickname', '');
			$mailbody = str_replace('\\','',$this->getRequest()->getParam('mailbody', ''));
			$arr = array('from'=>$from, 'subject'=>$subject, 'nickname'=>$nickname, 'mailbody'=>$mailbody);
			
			file_put_contents(MY_ROOT.'/html/upload/game/sendmail.txt', serialize($arr));
			//$this->getHelper('layout')->disableLayout();
			$this->getHelper('viewRenderer')->setNoRender();			
			if ($this->getRequest()->getParam('issend')) {
				$game = new CGame();
				$joinerList = $game->fetchAll();
				foreach ($joinerList as $v) {
					$mail = new CMail();
					$mailbody1 = preg_replace('/{email}/', $v->joineremail, $mailbody);
					$mail->sendEmail($from, $v->joineremail, $subject, $mailbody1);
					echo $v->joineremail . '<br/>';
				}
			}
			$this->view->showMsg('发送','/administrator-game-sendmail');
		}
		
	}
}